package com.ultrawise.survey.core;

import javax.servlet.http.HttpServletRequest;

public class CommonFun
{

	/**
	 * 获取远程请求者的IP地址
	 * 
	 * @param request
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request)
	{
		String ip = request.getHeader("x-forwarded-for");
		// log.info("x-forwarded-for:" + ip);
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getHeader("X-Forwarded-For");
		}

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getHeader("Proxy-Client-IP");
			// log.info("Proxy-Client-IP:" + ip);
		}

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getHeader("WL-Proxy-Client-IP");
			// log.info("WL-Proxy-Client-IP:" + ip);
		}

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getRemoteAddr();
			// log.info("Wrequest.getRemoteAddr():" + ip);
		}

		if (ip != null && ip.indexOf(",") != -1)
		{
			// log.info("IP:" + ip);
			String[] ips = ip.split(",");
			if (ips.length > 0 && !ips[0].equalsIgnoreCase("unknown"))
			{
				ip = ips[0];
				// log.info("X-Forwarded-For[0]:" + ip);
			}
		}

		return ip;
	}

}
